package org.mnswf.services {
	
	import com.dannypatterson.config.Settings;
	import com.dannypatterson.remoting.FaultEvent;
	import com.dannypatterson.remoting.ResultEvent;
	import com.dannypatterson.remoting.ServiceProxy;
	
	import flash.events.EventDispatcher;
	
	import org.mnswf.model.CertificationExam;
	import org.mnswf.model.Event;
	
	public class GetCertificationExamsService extends EventDispatcher implements IService {
	
		private var service:ServiceProxy;
		
		public function GetCertificationExamsService() {
			service = new ServiceProxy(Settings.getInstance().remotingGateway, Settings.getInstance().certificationService);
			service.addEventListener(ResultEvent.RESULT, onComplete, false, 0, true);
			service.addEventListener(FaultEvent.FAULT, onFault, false, 0, true);
		}
		
		private function onComplete(resultEvent:ResultEvent):void {
			var certifications:Array = new Array();
			for(var i:Number = 0; i < resultEvent.result.length; i++) {
				var certification:CertificationExam = new CertificationExam();
				certification.parseTransferObject(resultEvent.result[i]);
				certifications.push(certification);
			}
			dispatchEvent(new ResultEvent(ResultEvent.RESULT, resultEvent.bubbles, resultEvent.cancelable, certifications));
		}
		
		private function onFault(event:FaultEvent):void {
			dispatchEvent(new FaultEvent(FaultEvent.FAULT, event.bubbles, event.cancelable, event.fault));
		}
		
		public function execute():void {
			service.getCertificationExams();
		}
		
	}
}